const {useTransaction, pool} = require("../../js/service/mysqlService.js");
const normalResult = require("../../js/normalResult.js");
const config = require("../../js/config.js");
const {baseModel} = require("../../js/service/baseModel");
let result = JSON.parse(normalResult)

module.exports = {
    // http://localhost:9024/manage/example/getAll
    getAll: async (request, response) => {
        let sql = 'select id,name,price from mo_goods order by id desc limit 4';
        let res = await baseModel.getAll(sql)
        return response.showResult(res)
    },

    // http://localhost:9024/manage/example/getDataById?id=70
    getDataById: async (request, response) => {
        let {id} = request.query;
        let sql = 'select id, name, price from mo_goods';
        let res = await baseModel.getDataById(sql, id)
        return response.showResult(res)
    },

    // http://localhost:9024/manage/example/add
    // post {"name":"商品名称","price":111}
    add: async (request, response) => {
        let res = await baseModel.add('mo_goods', request.body);
        return response.showResult(res)
    },

    // http://localhost:9024/manage/example/updateById
    // post {"id":70,"name":"商品名称","price":111}
    updateById: async (request, response) => {
        let res = await baseModel.updateById('mo_goods', request.body);
        return response.showResult(res)
    },

    // http://localhost:9024/manage/example/notUseTransaction
    notUseTransaction: async (request, response) => {
        let res = await baseModel.add('mo_goods', {"name": "商品名称", "price": 111});
        await baseModel.updateById('mo_goods11', {"id": 70, "name": "70", "price": 70});
        //第二条sql语句执行失败，但是第一句sql语句已经执行成功
        return response.showResult(res)
    },

    // http://localhost:9024/manage/example/useTransaction
    useTransaction: async (request, response) => {
        try {
            await useTransaction(async (connection) => {
                await baseModel.add('mo_goods', {"name": "商品名称", "price": 222}, connection);
                await baseModel.updateById('mo_goods11', {"id": 70, "name": "70", "price": 70}, connection);
            });
            let res = Object.assign(result, config.result.success)
            return response.success(res);
        } catch (err) {
            console.error("事务失败:", err);
            return response.error(config.result.transactionError);
        }
    },

    // http://localhost:9024/manage/example/getPagination
    // post {"page":1,"size":10}
    getPagination: async (request, response) => {
        const page = parseInt(request.body.page) || 1;
        const size = parseInt(request.body.size) || 10;
        let totalSql = "SELECT COUNT(*) AS total FROM mo_goods";
        let sql = 'select id, name, price from mo_goods';
        let res = await baseModel.getPagination(sql, totalSql, page, size);
        return response.showResult(res)
    },

    // http://localhost:9024/manage/example/realDel?id=481
    realDel: async (request, response) => {
        let {id} = request.query;
        let res = await baseModel.realDelById('mo_goods', id);
        return response.showResult(res)
    },

}